#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "uart_cmd.h"
#include "driver/gpio.h"
#include "AS608.h"
#include <string.h>
#include "JQ8900.h"
#include "spiffs.h"
#include "wifi.h"
static QueueHandle_t uart_queue;
uart_event_t screen_ev;
uint8_t uart_screen_buffer[1024];
char* send_settings_head = "settings.vol.val=";
char* send_settings_tail = "\xff\xff\xff";
char* send_manager_suc = "page manage\xff\xff\xff";
char* send_head = "administrater.t1.txt=\"";
uint8_t send_manager_fail[] = {0xC4,0xFA,0xB2,0xBB,0xCA,0xC7,0xB9,0xDC,0xC0,0xED,0xD4,0xB1,0xA3,0xA1};
uint8_t send_manager_unknown[] = {0xC3,0xBB,0xD3,0xD0,0xC4,0xFA,0xB5,0xC4,0xD6,0xB8,0xCE,0xC6,0xA3,0xA1};
char* send_tail_admin = "\"\xff\xff\xff";


void User_Uart1_Init()
{
    uart_config_t uart_structure = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .parity = UART_PARITY_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
        .stop_bits = UART_STOP_BITS_1,
    };
    uart_param_config(SCREEN_UART,&uart_structure);
    uart_set_pin(SCREEN_UART,SCREEN_TX,SCREEN_RX,UART_PIN_NO_CHANGE,UART_PIN_NO_CHANGE);
    uart_driver_install(SCREEN_UART,1024,1024,20,&uart_queue,0);
}

uint8_t Cmd_Judge()
{
    if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
    {
            if(screen_ev.type == UART_DATA)
            {
                ESP_LOGI("uart1","recieve len : %i",screen_ev.size);
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                if(uart_screen_buffer[0] == 0xAA && uart_screen_buffer[1] == 0x00 && uart_screen_buffer[3] == 0xBB)
                {
                    if(uart_screen_buffer[2] == 0x01)
                    {
                        return 1;
                    }
                    else if(uart_screen_buffer[2] == 0x02)
                    {
                        return 2;
                    }
                    else if(uart_screen_buffer[2] == 0x03)
                    {
                        return 3;
                    }
                    else if(uart_screen_buffer[2] == 0x04)
                    {
                        return 4;
                    }
                    else if(uart_screen_buffer[2] == 0x05)
                    {
                        return 5;
                    }
                    else if(uart_screen_buffer[2] == 0x06)
                    {
                        return 6;
                    }
                    else if(uart_screen_buffer[2] == 0x0A)
                    {
                        return 0x0A;
                    }
                    else if(uart_screen_buffer[2] == 0x0D)
                    {
                        return 0x0D;
                    }
                    else if(uart_screen_buffer[2] == 0x10)
                    {
                        return 0x10;
                    }
                }
                else if(uart_screen_buffer[0] == 0x04 && uart_screen_buffer[1] == 0xFF && uart_screen_buffer[2] == 0xFF && uart_screen_buffer[3] == 0xFF)
                {
                    return 0xFF;
                }
            }
    }
    return 0;
}


size_t Register_finger_name(uint16_t ID,uint8_t position,char* name,Infomation_t* prevous,esp_vfs_spiffs_conf_t config,uint16_t* ID_arr)
{
    uint8_t i = 0;
    if(Fingerprint_Register(ID) == SUCCESS)
    {
        if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
        {
            if(screen_ev.type == UART_DATA)
            {
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                for(i = 0;i < screen_ev.size - 1;i++)
                {
                    name[i] = uart_screen_buffer[i];
                }
                position = uart_screen_buffer[screen_ev.size - 1];
                Personal_Information_create(name,ID,position,(uint8_t)screen_ev.size - 1,prevous);
                Information_Write_All(prevous,config,ID_arr);
            }
        }
    }
    return screen_ev.size - 1;
}

void Finger_manage(Infomation_t* previous,uint16_t* ID,esp_vfs_spiffs_conf_t config,uint8_t* id_num)
{
    uint16_t id = 0;
    uint8_t i = 0;
    while(1)
    {
        if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
        {
            if(screen_ev.type == UART_DATA)
            {
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                if(uart_screen_buffer[0] == 0xAA && uart_screen_buffer[1] == 0x00 && uart_screen_buffer[3] == 0xBB)
                {
                    if(uart_screen_buffer[2] == 0xFF)
                    {
                        break;
                    }
                    else if(uart_screen_buffer[2] == 0x0B)
                    {
                        id++;
                        Personal_Information_Search_manager(id,previous);
                    }
                    else if(uart_screen_buffer[2] == 0x12)
                    {
                        // if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(20000)))
                        // {
                        //     if(screen_ev.type == UART_DATA)
                        //     {
                        //         uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                        //         for(i = 0;i < screen_ev.size;i++)
                        //         {
                        //             name[i] = uart_screen_buffer[i];
                        //         }
                        //         Personal_Information_create(name,ID,position,(uint8_t)screen_ev.size - 1,prevous);
                        //         Information_Write_All(prevous,config,ID_arr);
                        //     }
                        // }
                    }
                    else if(uart_screen_buffer[2] == 0x13)
                    {
                        
                    }
                    else if(uart_screen_buffer[2] == 0x0C)
                    {
                        if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(500)))
                        {
                            if(screen_ev.type == UART_DATA)
                            {
                                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                                if(uart_screen_buffer[1] == 0x00 && uart_screen_buffer[2] == 0x00 && uart_screen_buffer[3] == 0x00)
                                {
                                    ID[*id_num] = (uint16_t)uart_screen_buffer[0];
                                    Personal_Information_delete(ID[*id_num],previous);
                                    AS608_DeleteChar(ID[*id_num],1);
                                    Information_Write_All(previous,config,ID);
                                    *id_num = (*id_num) + 1;
                                }
                            }
                        }
                    }
                }
                else if(uart_screen_buffer[1] == 0x00 && uart_screen_buffer[2] == 0x00 && uart_screen_buffer[3] == 0x00)
                {
                    id = (uint16_t)uart_screen_buffer[0];
                    Personal_Information_Search_manager(id,previous);
                }
            }
        }
    }
}

void System_clock_config()
{
    if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
    {
            if(screen_ev.type == UART_DATA)
            {
                if(uart_screen_buffer[0] == 0x04 && uart_screen_buffer[1] == 0xFF && uart_screen_buffer[2] == 0xFF && uart_screen_buffer[3] == 0xFF)
                {
                    return ;
                }
                else 
                {
                    uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(500));
                    struct tm timeinfo;
                    timeinfo.tm_year = 2000 - 1900 + (uart_screen_buffer[0] - '0') * 10 + (uart_screen_buffer[1] - '0');  // 设置年份
                    timeinfo.tm_mon = (uart_screen_buffer[2] - '0') * 10 + (uart_screen_buffer[3] - '0');  // 设置月份
                    timeinfo.tm_mday = (uart_screen_buffer[4] - '0') * 10 + (uart_screen_buffer[5] - '0');  // 设置日期
                    timeinfo.tm_hour = (uart_screen_buffer[6] - '0') * 10 + (uart_screen_buffer[7] - '0');  // 设置小时
                    timeinfo.tm_min = (uart_screen_buffer[8] - '0') * 10 + (uart_screen_buffer[9] - '0');  // 设置分钟
                    timeinfo.tm_sec = (uart_screen_buffer[10] - '0') * 10 + (uart_screen_buffer[11] - '0');  // 设置秒
                    setenv("TZ", "CST-8", 1);  // 设置时区为中国标准时间
                    tzset();  // 使时区设置生效

                    const time_t now = mktime(&timeinfo);  // 将tm结构体转换为time_t格式
                    struct timeval tv = { .tv_sec = now };  // 定义一个timeval结构体变量tv，并设置其秒数
                    settimeofday(&tv, NULL);  // 设置系统时间
                }
                
            }
    }
}

uint8_t Card_clock_set(Card_clock_t* set_time,uint8_t* card_mode)
{
    if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
    {
            if(screen_ev.type == UART_DATA)
            {
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(500));
                if(uart_screen_buffer[0] == 0xAA)
                {
                    if(uart_screen_buffer[1] == ONCE)
                    {
                        set_time->month[0] = uart_screen_buffer[2];
                        set_time->month[1] = uart_screen_buffer[3];
                        set_time->date[0] = uart_screen_buffer[4];
                        set_time->date[1] = uart_screen_buffer[5];
                        set_time->hour[0] = uart_screen_buffer[6];
                        set_time->hour[1] = uart_screen_buffer[7];
                        set_time->min[0] = uart_screen_buffer[8];
                        set_time->min[1] = uart_screen_buffer[9];
                        set_time->sec[0] = uart_screen_buffer[10];
                        set_time->sec[1] = uart_screen_buffer[11];
                        return ONCE;
                    }
                    else if(uart_screen_buffer[1] == EVERY_DAY)
                    {
                        set_time->hour[0] = uart_screen_buffer[2];
                        set_time->hour[1] = uart_screen_buffer[3];
                        set_time->min[0] = uart_screen_buffer[4];
                        set_time->min[1] = uart_screen_buffer[5];
                        set_time->sec[0] = uart_screen_buffer[6];
                        set_time->sec[1] = uart_screen_buffer[7];
                        if(screen_ev.size > 8)
                        {
                            set_time++;
                            set_time->hour[0] = uart_screen_buffer[8];
                            set_time->hour[1] = uart_screen_buffer[9];
                            set_time->min[0] = uart_screen_buffer[10];
                            set_time->min[1] = uart_screen_buffer[11];
                            set_time->sec[0] = uart_screen_buffer[12];
                            set_time->sec[1] = uart_screen_buffer[13];
                            if(screen_ev.size > 14)
                            {
                                set_time++;
                                set_time->hour[0] = uart_screen_buffer[14];
                                set_time->hour[1] = uart_screen_buffer[15];
                                set_time->min[0] = uart_screen_buffer[16];
                                set_time->min[1] = uart_screen_buffer[17];
                                set_time->sec[0] = uart_screen_buffer[18];
                                set_time->sec[1] = uart_screen_buffer[19];
                            }
                        }  
                        return EVERY_DAY;
                    }
                    else if(uart_screen_buffer[1] == WORK_DAY)
                    {
                        set_time->hour[0] = uart_screen_buffer[2];
                        set_time->hour[1] = uart_screen_buffer[3];
                        set_time->min[0] = uart_screen_buffer[4];
                        set_time->min[1] = uart_screen_buffer[5];
                        set_time->sec[0] = uart_screen_buffer[6];
                        set_time->sec[1] = uart_screen_buffer[7];
                        if(screen_ev.size > 8)
                        {
                            set_time++;
                            set_time->hour[0] = uart_screen_buffer[8];
                            set_time->hour[1] = uart_screen_buffer[9];
                            set_time->min[0] = uart_screen_buffer[10];
                            set_time->min[1] = uart_screen_buffer[11];
                            set_time->sec[0] = uart_screen_buffer[12];
                            set_time->sec[1] = uart_screen_buffer[13];
                            if(screen_ev.size > 14)
                            {
                                set_time++;
                                set_time->hour[0] = uart_screen_buffer[14];
                                set_time->hour[1] = uart_screen_buffer[15];
                                set_time->min[0] = uart_screen_buffer[16];
                                set_time->min[1] = uart_screen_buffer[17];
                                set_time->sec[0] = uart_screen_buffer[18];
                                set_time->sec[1] = uart_screen_buffer[19];
                            }
                        }
                        return WORK_DAY;
                    }
                }
                else if(uart_screen_buffer[0] == 0x04 && uart_screen_buffer[1] == 0xFF && uart_screen_buffer[2] == 0xFF && uart_screen_buffer[3] == 0xFF)
                {
                    return *card_mode;
                }
            }
    }
    return *card_mode;
}


uint8_t User_set_vol(uint8_t now_vol)
{
    uint8_t arr[2];
    arr[0] = now_vol / 10 + '0';
    arr[1] = now_vol % 10 + '0';
    uart_write_bytes(SCREEN_UART,send_settings_head,strlen(send_settings_head));
    uart_write_bytes(SCREEN_UART,arr,2);
    uart_write_bytes(SCREEN_UART,send_settings_tail,strlen(send_settings_tail));
    if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(5000)))
    {
            if(screen_ev.type == UART_DATA)
            {
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                Vol_Set(uart_screen_buffer[0]);
                return uart_screen_buffer[0];
            }
    }
    return now_vol;
}

void Log_Information_Show(Infomation_t* previous,esp_vfs_spiffs_conf_t config,uint16_t* ID)
{
    uint16_t id = 0;
    uint8_t page_num = 0;
    while(1)
    {
        if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(portMAX_DELAY)))
        {
            if(screen_ev.type == UART_DATA)
            {
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                if(uart_screen_buffer[0] == 0xAA && uart_screen_buffer[1] == 0x00 && uart_screen_buffer[3] == 0xBB)
                {
                    if(uart_screen_buffer[2] == 0xFF)
                    {
                        break;
                    }
                    else if(uart_screen_buffer[2] == 0x0B)
                    {
                        id++;
                        Personal_Information_Search_Log(id,previous,0);
                    }
                    else if(uart_screen_buffer[2] == 0x0D)
                    {
                        if(page_num == 29)
                        {
                            page_num = 0;
                        }
                        else
                            page_num++;
                        Personal_Information_Search_Log(id,previous,page_num);
                    }
                    else if(uart_screen_buffer[2] == 0x0E)
                    {
                        Information_Write_All(previous,config,ID);
                    }
                }
                else if(uart_screen_buffer[1] == 0x00 && uart_screen_buffer[2] == 0x00 && uart_screen_buffer[3] == 0x00)
                {
                    id = (uint16_t)uart_screen_buffer[0];
                    Personal_Information_Search_Log(id,previous,0);
                }
            }
        }
    }
}

void Manager_Finger_Search(Infomation_t* previous,SearchResult_t * SearchResult)
{
    uint8_t ensure;
    while(1)
    {
        if(pdTRUE == xQueueReceive(uart_queue,&screen_ev,pdMS_TO_TICKS(1000)))
        {
            if(screen_ev.type == UART_DATA)
            {
                ESP_LOGI("uart1","recieve len : %i",screen_ev.size);
                uart_read_bytes(SCREEN_UART,&uart_screen_buffer,screen_ev.size,pdTICKS_TO_MS(50));
                if(uart_screen_buffer[0] == 0xAA && uart_screen_buffer[1] == 0x00 && uart_screen_buffer[3] == 0xBB)
                {
                    if(uart_screen_buffer[2] == 0x11)
                    {
                        return;
                    }
                }
                else if(uart_screen_buffer[0] == 0x04 && uart_screen_buffer[1] == 0xFF && uart_screen_buffer[2] == 0xFF && uart_screen_buffer[3] == 0xFF)
                {
                    return;
                }
            }
        }

        if(Judge_FingerPress())
        {
            ensure = AS608_GetImage();
            if(ensure == 0x00)
            {
                ensure = AS608_GenChar(0x01);
                if(ensure == 0x00)
                {
                    ensure = AS608_Search(0x01,0,300,SearchResult);
                    if(ensure == 0x00)
                    {
                        Infomation_t* current = previous;
                        while(current->ID != SearchResult->PageID && current->next != NULL)
                        {
                            current = current->next;
                        }
                        if(current->next == NULL && current->ID != SearchResult->PageID)
                        {
                            uart_write_bytes(SCREEN_UART,send_head,strlen(send_head));
                            uart_write_bytes(SCREEN_UART,send_manager_unknown,sizeof(send_manager_unknown));
                            uart_write_bytes(SCREEN_UART,send_tail_admin,strlen(send_tail_admin));
                            
                        }
                        if(current->position == 0)
                        {
                            uart_write_bytes(SCREEN_UART,send_manager_suc,strlen(send_manager_suc));
                            Specify_Repertoire(0x0F);
                            vTaskDelay(pdMS_TO_TICKS(1000));
                            return;
                        }
                        else
                        {
                            uart_write_bytes(SCREEN_UART,send_head,strlen(send_head));
                            uart_write_bytes(SCREEN_UART,send_manager_fail,sizeof(send_manager_fail));
                            uart_write_bytes(SCREEN_UART,send_tail_admin,strlen(send_tail_admin));
                        }
                    }
                }
            }
        }
    }
}